package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.ism.model.IsmAppSetting;
import com.sec.etech.ism.model.IsmServiceDef;
import com.sec.etech.ism.service.IsmAppSettingService;
import com.sec.etech.ism.service.IsmCommonService;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmServiceDefService;
import com.sec.etech.ism.util.IsmStringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class IsmSendUnifiedSubComInfoServiceImpl implements IsmMethodService {
    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    IsmCommonService ismCommonService;
    @Resource
    IsmAppSettingService ismAppSettingService;
    @Resource
    IsmServiceDefService ismServiceDefService;

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject busObj) throws Exception {
        JSONObject result = new JSONObject();
        String msg = this.sendUnifiedSubComInfo(sysCode, serviceCode, busObj);
        result.put("flag", msg);
        result.put("msg", msg);
        return result;
    }

    private String sendUnifiedSubComInfo(String sysCode, String serviceCode, JSONObject busObj) {
        Map<String, String> logMap = new HashMap<>(5);
        //服务信息
        IsmServiceDef serviceDef = null;
        try {
            serviceDef = ismServiceDefService.getByCode(serviceCode);
            //调用的应用信息
            IsmAppSetting appSetting = ismAppSettingService.getByCode(sysCode);
            String url = appSetting.getAppAddress() + "prodInfo/sleeveInconsistentRefresh";
            logMap.put("appName", this.methodName());
            logMap.put("url", url);
            String sql = "select partsnum,partsname,parts_SAPnum from prod_parts where partsid=?";
            Map<String, Object> partsMap = jdbcTemplate.queryForMap(sql, busObj.getString("partsid"));
            JSONObject params = new JSONObject();
            params.put("partsNum", partsMap.get("partsnum"));
            params.put("partsName", partsMap.get("partsname"));
            params.put("partsSAPnum", IsmStringUtil.getStrNullToEmpty(partsMap.get("parts_SAPnum")));
            partsMap.clear();
            sql = "select t1.numcode,t1.amount,t2.partsnum,t2.partsname,t2.parts_SAPnum " +
                    "from prod_parts_detail t1, prod_parts t2 where t1.partsid=t2.partsid and t1.parentid=? and t1.active=1";
            List<Map<String, Object>> subList = jdbcTemplate.queryForList(sql, busObj.getString("partsid"));
            List<JSONObject> subPartsList = new ArrayList<>();
            if (subList != null && subList.size() > 0) {
                for (Map<String, Object> map : subList) {
                    JSONObject object = new JSONObject();
                    object.put("partsNum", map.get("partsnum"));
                    object.put("partsName", map.get("partsname"));
                    object.put("partsSAPnum", IsmStringUtil.getStrNullToEmpty(map.get("parts_SAPnum")));
                    object.put("numCode", map.get("numcode"));
                    object.put("amount", map.get("amount"));
                    subPartsList.add(object);
                }
            }
            params.put("subPartsList", subPartsList);
            ismCommonService.checkResult(url, params.toJSONString(), serviceDef, null, busObj.toJSONString(), this.methodName(), logMap);
            return "success";
        } catch (Exception e) {
            log.error("分部套修改结果推送PCS接口异常。serviceDef.getCode()={}，serviceDef.getName()={}，logMap={}",
                    serviceDef.getCode(), serviceDef.getName(), logMap);
            e.printStackTrace();
            return "分部套修改结果推送PCS接口异常，请联系管理员处理！";
        }
    }

    @Override
    public String methodName() {
        return "sendUnifiedSubComInfo";
    }
}
